//
//  HWVoiceRoomGiftView.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/8/27.
//

import UIKit

class HWVoiceRoomGiftView: MMPopupView {
    private var tapBgView:UIView!
    private var bgView: UIView!
    var topView:HWVoiceRoomGiftTopView!
    var centerView:HWVoiceRoomGiftSegmentView!

    var bottomView:UIView!
    var bottomLineView:UIView!
    var diamondsImage:UIImageView!
    /// 钻石余额
    var diamondsLabel:UILabel!
    /// 充值
    var rechargeButton:UIButton!
    /// 发送视图
    var sendView:HWVoiceRoomGiftSendView!
    
    var sendGift:((_:[HWVoiceRoomSeatModel],_:HWVoiceRoomGiftModel,_:String)->())?
    
    var sendToUsers:[HWVoiceRoomSeatModel] = Array()
    var users:[String] = Array()
    let httpManager = HWGameLiveHttpManager()
    var urlString = ""
    var seatModels:[HWVoiceRoomSeatModel]?{
        didSet {
            topView.seatModels = seatModels
        }
    }
    var giftModels:[HWVoiceRoomGiftListModel]? {
        didSet{
            centerView.giftModels = giftModels
        }
    }
    var willSendGiftModel:HWVoiceRoomGiftModel = HWVoiceRoomGiftModel()
    
    var rechargeButtonClick:((_ :String)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
        addAction()
        
        loadData()
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
//        bgView.addCorner(conrners: [.topLeft, .topRight], radius: 20)
        
    }

}
extension HWVoiceRoomGiftView {
    private func initUI() -> Void {
        type = MMPopupType.sheet
        backgroundColor = UIColor.clear
        attachedView = UIApplication.shared.windows.filter({ $0.isKeyWindow }).last?.rootViewController?.view
        tapBgView = UIView.init()
        tapBgView.backgroundColor = UIColor.clear
        addSubview(tapBgView)
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(bacgroundTap(_:)))
        tapBgView.addGestureRecognizer(tap)
        
//        bgView = UIView.init()
//        bgView.backgroundColor = .colorWithHexString(hexString: "#6930CF")
//        addSubview(bgView)
        
        
        topView = HWVoiceRoomGiftTopView()
        topView.backgroundColor = .colorWithHexString(hexString: "#6930CF")
        topView.layer.cornerRadius = 8
        addSubview(topView)
        
        centerView = HWVoiceRoomGiftSegmentView()
        centerView.backgroundColor = .colorWithHexString(hexString: "#6930CF")
        addSubview(centerView)
        
        bottomView = UIView()
        bottomView.backgroundColor = .colorWithHexString(hexString: "#6930CF")
        addSubview(bottomView)
        
        bottomLineView = UIView()
        bottomLineView.backgroundColor = .colorWithHexString(hexString: "#7D4DD2")
        bottomView.addSubview(bottomLineView)
        
        diamondsImage = UIImageView()
        diamondsImage.backgroundColor = .clear
        diamondsImage.image = UIImage(named: "diamonds")
        bottomView.addSubview(diamondsImage)
        
        diamondsLabel = HWLabel.createBoldLabel(title: "0", color: "#E7E7E7", font: 14)
        bottomView.addSubview(diamondsLabel)
        
        rechargeButton = UIButton.init(type: UIButton.ButtonType.custom)
        rechargeButton.setTitle("充值", for: UIControl.State.normal)
        rechargeButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
        rechargeButton.setTitleColor(.colorWithHexString(hexString: "#FFFFFF"), for: UIControl.State.normal)
        rechargeButton.addTarget(self, action: #selector(rechargeButtonClick(_:)), for: UIControl.Event.touchUpInside)
        bottomView.addSubview(rechargeButton)
        
        sendView = HWVoiceRoomGiftSendView()
        bottomView.addSubview(sendView)
        
    }
    
    private func initFrame() -> Void {
        snp.makeConstraints { make in
            make.width.equalTo(screenWidth)
            make.height.equalTo(screenHeight)
        }
        
        tapBgView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalTo(0)
        }
        
//        bgView.snp.makeConstraints { make in
//            make.left.right.bottom.equalTo(0)
//            make.height.equalTo(369)
//        }
        
        bottomView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(0)
            make.height.equalTo(80)
        }
        
        centerView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.height.equalTo(369-80)
            make.bottom.equalTo(bottomView.snp.top)
        }
        topView.snp.makeConstraints { make in
            make.bottom.equalTo(centerView.snp.top).offset(-8)
            make.left.right.equalTo(0)
            make.height.equalTo(48)
        }
        
        bottomLineView.snp.makeConstraints { make in
            make.top.left.right.equalTo(0)
            make.height.equalTo(1)
        }
        
        diamondsImage.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.bottom.equalTo(-47)
            make.width.equalTo(17)
            make.height.equalTo(15)
        }
        
        diamondsLabel.snp.makeConstraints { make in
            make.left.equalTo(diamondsImage.snp.right).offset(5)
            make.centerY.equalTo(diamondsImage)
        }
        
        rechargeButton.snp.makeConstraints { make in
            make.left.equalTo(diamondsLabel.snp.right).offset(20)
            make.centerY.equalTo(diamondsLabel)
        }
        
        sendView.snp.makeConstraints { make in
            make.centerY.equalTo(rechargeButton)
            make.right.equalTo(-15)
        }
    }
}
extension HWVoiceRoomGiftView {
    @objc func bacgroundTap(_ tap:UITapGestureRecognizer) -> Void {
        hide()
//        userDetailViewHiddenCallBack?()
    }
    
    @objc func selectAllButtonClick(_ button:UIButton) -> Void {
        
    }
    
    @objc func rechargeButtonClick(_ button:UIButton) -> Void {
        if urlString.count > 0{
            rechargeButtonClick?(urlString)
            
            
        }
        
    }
    
    func addAction() -> Void {
        topView.didSelectUser = {
            users in
            //要送的人
            self.sendToUsers = users
            
        }
        centerView.didSelectGift = {
            mod in
            ///选中的礼物
            self.willSendGiftModel = mod
        }
        
        sendView.sendButtonClick = {
            num in
            if self.sendToUsers.count != 0 {
             
            }else{
                self.showHUD(title: "请选择要送的人")
                return
            }
            if self.willSendGiftModel.id != nil && self.willSendGiftModel.id != "" {
                
            }else{
                self.showHUD(title: "请选择要送礼物")
                return
            }
            self.users.removeAll()
            for user  in self.sendToUsers {
                self.users.append(user.seatUser?.userId ?? "")
            }
            self.sendGift?(self.sendToUsers,self.willSendGiftModel,num)
            
                
        }
    }
}

extension HWVoiceRoomGiftView {
    func loadData() -> Void {
        httpManager.getDiamondNum { dic in
            self.diamondsLabel.text = dic["coin"] as? String
            self.urlString = dic["url"] as? String ?? ""
        } failure: { error in
            
        }

    }
}

